package com.zhen.activiti.web;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

@Controller
@RequestMapping(value = "/oa")
public class DeploymentController extends AbstractController {

    @RequestMapping(value = "process-list")
    public ModelAndView processList(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ModelAndView modelAndView = new ModelAndView("process-list");
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
        modelAndView.addObject("processDefinitionList",processDefinitionList);
        return modelAndView;
    }
    @RequestMapping(value = "/deploy")
    public String deploy(@RequestParam(value = "file")MultipartFile multipartFile){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String fileName = multipartFile.getOriginalFilename();
        try{
            InputStream inputStream = multipartFile.getInputStream();
            String extension = FilenameUtils.getExtension(fileName);
            DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
            if(extension.equals("zip") || extension.equals("bar")){
                ZipInputStream zip = new ZipInputStream(inputStream);
                deploymentBuilder.addZipInputStream(zip);
            }else{
                deploymentBuilder.addInputStream(fileName,inputStream);
            }
            deploymentBuilder.deploy();
        }catch (Exception e){
            logger.error("error on deploy process,because of file input stream");
        }
        return "redirect:process-list";

    }

    @RequestMapping(value = "/read-resource")
    public void readResource(@RequestParam(value = "pdid") String processDefinitionId,
                             @RequestParam(value="resourceName") String resourceName,
                             HttpServletResponse httpServletResponse) throws Exception{
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionId(processDefinitionId).singleResult();
        logger.info("资源图片" + processDefinition.getDiagramResourceName());
        System.out.println("资源图片" + processDefinition.getDiagramResourceName());
        InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),resourceName);
        byte[] b = new byte[1024];
        int len = -1;
        while((len = inputStream.read(b,0,1024)) != -1){
            httpServletResponse.getOutputStream().write(b,0,len);
        }
    }

    @RequestMapping(value = "delete-deployment")
    public String deleteProcessDefinition(@RequestParam(value = "deploymentId") String deploymentId){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment(deploymentId,true);
        return "redirect:process-list";
    }
}
